﻿Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime

''' <summary>
''' Author Rajab Ma 
''' majianle@gmail.com
''' </summary>
''' <remarks></remarks>
Public Class EntityModify

#Region "第二章第2节 修改圆形对象的基本属性"
    ''' <summary>
    ''' 修改实体所在的层
    ''' </summary>
    ''' <param name="entID">实体Id</param>
    ''' <param name="layerName">层名字</param>
    ''' <remarks></remarks>
    Public Shared Sub ChangeLayer(ByVal entID As ObjectId, ByVal layerName As String)


        Dim db As Database = HostApplicationServices.WorkingDatabase
        Using trans As Transaction = db.TransactionManager.StartTransaction()
            Try
                Dim ent As Entity = DirectCast(trans.GetObject(entID, OpenMode.ForWrite), _
                                          Entity)
                ent.Layer = layerName
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                Util.Output(ex.Message)
            Finally
                trans.Commit()
            End Try

        End Using

    End Sub
#End Region

#Region "旋转 平移 缩放实体"
    ''' <summary>
    ''' 旋转实体
    ''' </summary>
    ''' <param name="entId">实体的对象Id</param>
    ''' <param name="ptBase">旋转基点</param>
    ''' <param name="rotation">旋转弧度</param>
    ''' <remarks></remarks>
    Public Shared Sub Rotate(ByVal entId As ObjectId, _
                                  ByVal ptBase As Point2d, _
                                  ByVal rotation As Double)



        Dim xform As Matrix3d = Matrix3d.Rotation(rotation, Vector3d.ZAxis, _
                                                  Calculation.Pt2dTo3d(ptBase))

        Dim db As Database = HostApplicationServices.WorkingDatabase
        Using trans As Transaction = db.TransactionManager.StartTransaction()
            Try
                Dim ent As Entity = DirectCast(trans.GetObject(entId, OpenMode.ForWrite, False), _
                                          Entity)
                ent.TransformBy(xform)
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                Throw ex
            Finally
                trans.Commit()
            End Try
        End Using

    End Sub

    ''' <summary>
    ''' 平移实体
    ''' </summary>
    ''' <param name="entid">实体Id</param>
    ''' <param name="ptBase">平移起始点</param>
    ''' <param name="ptDest">平移目标点</param>
    ''' <remarks></remarks>
    Public Shared Sub Move(ByVal entid As ObjectId, _
                                ByVal ptBase As Point3d, _
                                ByVal ptDest As Point3d)


        Dim vec As Vector3d = New Vector3d(ptDest.X - ptBase.X, ptDest.Y - ptBase.Y, _
                                           ptDest.Z - ptBase.Z)

        Dim xform As Matrix3d = Matrix3d.WorldToPlane(vec)

        Dim db As Database = HostApplicationServices.WorkingDatabase
        Using trans As Transaction = db.TransactionManager.StartTransaction()
            Try
                Dim ent As Entity = DirectCast(trans.GetObject(entid, OpenMode.ForWrite, False), _
                                               Entity)
                ent.TransformBy(xform)
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                Throw ex
            Finally
                trans.Commit()
            End Try
        End Using

    End Sub

    ''' <summary>
    ''' 绽放实体
    ''' </summary>
    ''' <param name="entid">实体Id</param>
    ''' <param name="ptBase">平移基点</param>
    ''' <param name="scaleFactor">缩放因子</param>
    ''' <remarks></remarks>
    Public Shared Sub Scale(ByVal entid As ObjectId, _
                                ByVal ptBase As Point3d, _
                                ByVal scaleFactor As Double)

        Dim xform As Matrix3d = Matrix3d.Scaling(scaleFactor, ptBase)

        Dim db As Database = HostApplicationServices.WorkingDatabase
        Using trans As Transaction = db.TransactionManager.StartTransaction()
            Try
                Dim ent As Entity = DirectCast(trans.GetObject(entid, OpenMode.ForWrite, False), _
                                               Entity)
                ent.TransformBy(xform)
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                Throw ex
            Finally
                trans.Commit()
            End Try
        End Using

    End Sub

#End Region

End Class
